/*
 * ============================================================================
 *
 *       Filename:  main.c
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2014年08月18日 17时30分36秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  jianxi sun (jianxi), ycsunjane@gmail.com
 *   Organization:  
 *
 * ============================================================================
 */
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include "arg.h"
#include "common.h"
#include "system.h"
#include "aphash.h"
#include "message.h"
#include "aclua.h"
#include "net.h"

static void getuuid(char *uuid)
{
	assert(luahandle.L_main != NULL);
	lua_State *L = luahandle.L_main;

	memset(uuid, 0, UUID_LEN);
	aclua_getglobal(L, "acuuid");
	aclua_pcall(L, 0, 1);

	if(!lua_isstring(L, -1)) {
		sys_err("funciton <acuuid> must return a string\n");
		exit(-1);
	}
	strncpy(uuid, lua_tostring(L, -1), UUID_LEN-1);
	lua_pop(L, 1);
}

void ac_init()
{
	getuuid(gconf.acuuid);
}

int main(int argc, char *argv[])
{
	proc_args(argc, argv);
	if(daemon_enable) daemon(0,0);

	aclua_init();

	gconf_init(args_info.nic_arg,
		args_info.acaddr_arg,
		args_info.port_arg);
	ac_init();
	aphash_init();
	message_init();
	net_init();

	pause();
	return 0;
}
